﻿using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using signalr.service.broker.ConnectionSql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace signalr.service.broker.BackgroundServices
{
    public class Sd_BackgroundService : BackgroundService
    {
        private readonly ILogger<Sd_BackgroundService> _logger;
        private readonly SqlDependencyContext dependencyContext;
        public Sd_BackgroundService(ILogger<Sd_BackgroundService> logger, SqlDependencyContext _dependencyContext)
        {
            _logger = logger;
            dependencyContext = _dependencyContext;
        }
        public override void Dispose()
        {
            dependencyContext.Termination();
            base.Dispose();
        }

        public override Task StartAsync(CancellationToken cancellationToken)
        {
            dependencyContext.init();
            _logger.LogInformation("启动任务:"+DateTime.Now);
            return base.StartAsync(cancellationToken);
        }

        public override Task StopAsync(CancellationToken cancellationToken)
        {
            dependencyContext.Termination();
            _logger.LogWarning("结束任务:"+DateTime.Now);
            return base.StopAsync(cancellationToken);
        }

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            dependencyContext.Update();
            _logger.LogInformation("执行任务:" + DateTime.Now);
            return Task.CompletedTask;
        }
    }
}
